/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-22
 * Time: 下午9:30
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.system.user.UserManageController',{
    extend:'Ext.app.Controller',
    models:['system.user.UserManageModel'],
    views:['system.user.UserManageView'],
    refs:[
        {ref:'userManageView',selector:'userManageView'},
        {ref:'userGridPanel',selector:'userGridPanel'},
        {ref:'userQueryFormPanel',selector:'userQueryFormPanel'},
        {ref:'userPopUpPanel',selector:'userPopUpPanel'},
        {ref:'userPopUpWindow',selector:'userPopUpWindow'}
    ],
    init:function (){
        var controller = this;
        this.control({
            'userManageView':{
                afterrender:function(){
                }
            },
            'userQueryFormPanel button[action=query]':{
                click:function(){
                    this.getUserGridPanel().getStore().loadPage(1);
                }
            },
            'userQueryFormPanel button[action=add]':{
                click:function(){
                    this.opType = "add";
                    var _popUpWindow = this.getUserPopUpWindow();
                    if(_popUpWindow == undefined){
                        _popUpWindow = Ext.create('Airways.view.system.user.PopUpWindow');
                    }
                    _popUpWindow.setTitle("添加用户");
                    _popUpWindow.setIconCls("add");
                    var password = controller.getUserPopUpPanel().down("textfield[name=password]");
                    var confirmpassword = controller.getUserPopUpPanel().down("textfield[name=confirmpassword]");
                    password.setDisabled(false);
                    password.setVisible(true);
                    confirmpassword.setDisabled(false);
                    confirmpassword.setVisible(true);
                    _popUpWindow.show();
                }
            },
            'userQueryFormPanel button[action=edit]':{
                click:function(){
                    var record = this.record;
                    if(record != null && record != ""){
                        controller.opType = "edit";
                        var _popUpWindow = controller.getUserPopUpWindow();
                        if(_popUpWindow == undefined){
                            _popUpWindow = Ext.create('Airways.view.system.user.PopUpWindow');
                        }
                        _popUpWindow.setTitle("编辑用户");
                        _popUpWindow.setIconCls("edit");
                        controller.getUserPopUpPanel().getForm().loadRecord(record);
                        var password = controller.getUserPopUpPanel().down("textfield[name=password]");
                        var confirmpassword = controller.getUserPopUpPanel().down("textfield[name=confirmpassword]");
                        password.setDisabled(true);
                        password.setVisible(false);
                        confirmpassword.setDisabled(true);
                        confirmpassword.setVisible(false);
                        _popUpWindow.show();
                    }else{
                        Ext.MessageBox.show({title:'提示',msg:'请先选中一行数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'userQueryFormPanel button[action=delete]':{
                click:function(){
                    var record = controller.record;
                    if(record != null && record != ""){
                        Ext.MessageBox.confirm("提示","确定要删除当前记录吗",function (e){
                            controller.opType = "delete";
                            if( e == "yes"){
                                var url = basePath+'system/user/deleteUser';
                                var params = new Object();
                                params.opType = controller.opType;
                                params.userId = record.data.userId;
                                params.version = record.data.version;
                                //调用ajax 发送数据到后台
                                callAjax(url,params,handleDeleteResult);
                            }
                        });
                    }else{
                        Ext.MessageBox.show({title:'提示',msg:'请先选择一行数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'userPopUpWindow button[action=save]':{
                click:function(){
                    var form = controller.getUserPopUpPanel().getForm();
                    if(form.isValid()){
                        //验证通过
                        var url = basePath+'system/user/saveUser';
                        var params = form.getFieldValues();
                        params.opType = controller.opType;
                        //调用ajax 发送数据到后台
                        callAjaxLocal(url,params,handleResult);
                    }else{
                        //验证失败
                        Ext.MessageBox.show({title:'提示',msg:'请按照提示填写数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'userPopUpWindow button[action=cancel]':{
                click:function(){
                    clearForm();
                }
            },
            'userPopUpWindow':{
                close:function(){
                    clearForm();
                }
            },
            'userGridPanel':{
                render:function(grid,opts){
                    var store=grid.getStore();
                    var _queryFormPanel = controller.getUserQueryFormPanel();
                    var button = _queryFormPanel.down("button[action=query]");
                    store.on({
                        load: function(){
                            button.setDisabled(false);
                        },
                        beforeload:function(){
                            controller.record = null;
                            button.setDisabled(true);
                            store.proxy.extraParams = _queryFormPanel.getForm().getFieldValues();
                        }
                    })
                },
                selectionchange: function(view, records) {
                    if (records[0]) {
                        this.record = records[0];
                    }
                }
            }
        });
        function handleResult(result){
            if(result.success){
                Ext.MessageBox.show({title:'提示',msg:'操作已成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK});
                clearForm();
                controller.getUserGridPanel().getStore().loadPage(1);
            }else{
                Ext.MessageBox.show({title:'提示',msg:'操作失败!',icon:Ext.MessageBox.ERROR,buttons:Ext.MessageBox.OK});
            }
        }
        function clearForm(){
            var form = controller.getUserPopUpPanel().getForm();
            controller.getUserPopUpWindow().hide();
            form.reset();
        }
        function handleDeleteResult(result){
            controller.getUserGridPanel().getStore().loadPage(1);
        }
    }
});
